package org.zlw.core.context;

import io.netty.channel.ChannelHandlerContext;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.function.Consumer;

public class BaseContext implements  IContext{
    //转发协议,final修饰,不变性
    protected final String protocol;
    //状态 ,volatile,多线程情况下
    protected  volatile  int status=IContext.Runing;

    //netty 上下文，从通道拿到的
    protected final ChannelHandlerContext nettyCtx;
    //上下文参数
    protected final Map<String,Object> attributes=new HashMap<String,Object>();


    //请求过程中发生的异常
    protected Throwable throwable;
    //是否保持长连接
    protected final  boolean keepAlice;
    //存放回调函数集合
    protected List<Consumer<IContext>> completedCallBacks;

    //是否已经释放资源  AtomicBoolean关键字，多线程，
    protected final AtomicBoolean requestReleased = new AtomicBoolean(false);

    public BaseContext(String protocol, ChannelHandlerContext nettyCtx,  boolean keepAlice) {
        this.protocol = protocol;
        this.nettyCtx = nettyCtx;
        this.keepAlice = keepAlice;
    }


    @Override
    public void runned() {
        status=IContext.Runing;
    }

    @Override
    public void writtened() {
status=IContext.Written;
    }

    @Override
    public void completed() {
status=IContext.Completed;
    }

    @Override
    public void terminated() {
        status=IContext.Terminated;
    }

    @Override
    public boolean isRunning() {
        return status == IContext.Runing;
    }

    @Override
    public boolean iswrittened() {
        return status == IContext.Written;
    }

    @Override
    public boolean iscompleted() {
        return  status == IContext.Completed;
    }

    @Override
    public boolean isterminated() {
        return  status == IContext.Terminated;
    }

    @Override
    public String getProtovol() {
        return this.protocol;
    }

    @Override
    public Object getRequset() {
        return null;
    }

    @Override
    public Object getResponse() {
        return null;
    }

    @Override
    public void setResponse(Object response) {

    }

    @Override
    public Throwable getThrowable() {
return this.throwable;
    }

    @Override
    public void setThrowable(Throwable throwable) {
this.throwable=throwable;
    }

    @Override
    public ChannelHandlerContext getNettyCtx() {
        return this.nettyCtx;
    }

    @Override
    public boolean isKeepAlive() {
        return this.keepAlice;
    }

    @Override
    public boolean releaseRequest() {
        return false;
    }

    @Override
    public void setCompletedCallBack(Consumer<IContext> consumer) {
        //判断回调函数是否为空
        if(completedCallBacks == null)
        {
            completedCallBacks=new ArrayList<>();
        }
        completedCallBacks.add(consumer);
    }

    @Override
    public void invokeCompletedCallBack(Consumer<IContext> consumer) {
        if(completedCallBacks!=null)
        {
            completedCallBacks.forEach(call->call.accept(this));
        }
    }
    /**
     *
     */

}
